package LCR;

public class twoSum {
    public int[] twoSum(int[] numbers, int target) {
        int[] ret = new int[2];
        int left = 0;
        int right = numbers.length - 1;
        while (left < right) {
            int tem = numbers[left] + numbers[right];
            if (tem == target) {
                ret[0] = left;
                ret[1] = right;
                break;
            }
            if (tem > target) {
                right--;
            }
            if (tem < target) {
                left++;
            }

        }
        return ret;

    }

    public int[] twoSum2(int[] numbers, int target) {
        for (int i = 0; i < numbers.length; ++i) {
            int low = i + 1, high = numbers.length - 1;
            while (low <= high) {
                int mid = (high - low) / 2 + low;
                if (numbers[mid] == target - numbers[i]) {
                    return new int[]{i, mid};
                } else if (numbers[mid] > target - numbers[i]) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }
        }
        return new int[]{-1, -1};
    }


}
